package edu.gatech.asthma.activity;

import java.util.ArrayList;
import java.util.List;

import edu.gatech.asthma.R;
import edu.gatech.asthma.database.DBAdapter;
import edu.gatech.asthma.database.ProfileVO;
import edu.gatech.asthma.ui_support.ResourceHelper;
import edu.gatech.asthma.ui_support.login.ProfilePicPageFragment;
import edu.gatech.asthma.ui_support.login.ViewPagerAdapter;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

/** 
 * Profile Login screen
 */
public class LoginActivity extends FragmentActivity {
	ViewPagerAdapter viewAdapter;
	ViewPager viewPager;
	DBAdapter database;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        
        database = new DBAdapter(this);
        database.open();

        List<Fragment> fragments = getProfiles();

        viewAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager = (ViewPager) findViewById(R.id.profile_pic_gallery);
        viewPager.setAdapter(viewAdapter);
        viewPager.setCurrentItem(0);
        
        //BUTTON LISTENERS
        final Button create = (Button) findViewById(R.id.button_create_profile);
        create.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent(LoginActivity.this, CreateProfileChildActivity.class);
            	LoginActivity.this.startActivity(i);
            }
        });
        
        Button login = (Button) findViewById(R.id.button_login);
        login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent(LoginActivity.this, HomeScreenActivity.class);
            	i.putExtra("ID", viewPager.getCurrentItem());
            	LoginActivity.this.startActivity(i);
            }
        });
        
        Button emergency = (Button) findViewById(R.id.button_emergency);
        emergency.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent(LoginActivity.this, ShowContactsActivity.class);
            	LoginActivity.this.startActivity(i);
            }
        });
        
        final ImageButton left = (ImageButton) findViewById(R.id.profile_select_left);
        viewAdapter.setListenerLeft(left,viewPager,null);
        
        final ImageButton right = (ImageButton) findViewById(R.id.profile_select_right);
        viewAdapter.setListenerRight(right,viewPager,null);
    }
	
	/**
	 * A helper method that accesses gets profile information from database helper class and
	 * populates Fragments (layouts) that will be displayed in the ViewPager
	 * @return profileList A list of Fragments with all profile information filled in
	 */
	private List<Fragment> getProfiles() {
		
		List<Fragment> profileList = new ArrayList<Fragment>();
		
		if (database.getNumProfiles() == 0) {
			profileList.add(ProfilePicPageFragment.newInstance(getString(R.string.login_no_profiles),R.drawable.icon_profile_boy,
					R.drawable.icon_profile_boy,R.drawable.icon_profile_boy,R.color.profile_color0,R.color.accent_color0));
			Button login = (Button) findViewById(R.id.button_login);
			login.setEnabled(false);
			login.setTextColor(getResources().getColor(R.color.light_gray));
		}
		
		int rIdTint=0, rIdAccent=0, rIdTop=0; //the resource ids of drawables in the res folder
		int rTintColor=0, rAccentColor=0;		//resource ids of colors
		ProfileVO profile; 	//current profile
		
		for (int i=0; i < database.getNumProfiles(); i++) {
			profile = database.getProfile(i);
			
			rIdTint = ResourceHelper.getImage("character"+profile.getPic()+"_tint");
			rIdAccent = ResourceHelper.getImage("character"+profile.getPic()+"_accent");
			rIdTop = ResourceHelper.getImage("character"+profile.getPic()+"_top");
			rTintColor = ResourceHelper.getColor("character_color"+profile.getTintColor());
			rAccentColor = ResourceHelper.getColor("accent_color"+profile.getAccentColor());
			
			profileList.add(ProfilePicPageFragment.newInstance(profile.getName(),rIdTint, rIdAccent, rIdTop,rTintColor,rAccentColor));
		}
		return profileList;
	}

}